package dale.web.link.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.validation.Errors;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.support.SessionStatus;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import dale.web.common.model.*;
import dale.web.common.model.Error;
import dale.web.link.model.Link;
import dale.web.link.service.LinkService;

@Controller
@RequestMapping("/link")
public class LinkController {

	@Autowired
	private LinkService service;

	@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
	@ExceptionHandler(Exception.class)
	public @ResponseBody Error handle(Exception ex) {
		ex.printStackTrace();
		return new Error(ex.getClass().getName(), ex.getMessage());
	}
	
	@RequestMapping(value={"/", "/list"}, method=RequestMethod.GET) 
	public String index() {
		return "/link/list";
	}
	
	@RequestMapping(value="/detail", method=RequestMethod.GET)
	public String detail() {
		return "/link/detail";
	}
	
	@RequestMapping(value="/edit", method=RequestMethod.GET)
	public String edit() {
		return "/link/edit";
	}

	@RequestMapping(value="/data", method=RequestMethod.GET)
	public @ResponseBody TableData<Link> getData() {
		return service.getData();
	}
	
	@RequestMapping(value="/search", method=RequestMethod.GET)
	public @ResponseBody PagedList<Link> search(@ModelAttribute Condition<Link> condition) {
		return service.search(condition);
	}
	
	@RequestMapping(value="/find", method=RequestMethod.GET)
	public @ResponseBody Link find(@RequestParam Integer no) {
		return service.find(no);
	}
	
	@RequestMapping(value="/categoryOptions", method=RequestMethod.GET)
	public @ResponseBody List<String> categoryOptions() {
		return service.categoryOptions();
	}
	
	@RequestMapping(value="/remove", method=RequestMethod.POST)
	public @ResponseBody String remove(@ModelAttribute Link item) {
		if (item == null || item.getNo() < 1) {
			throw new RuntimeException("INPUT ERROR");
		}
		
		service.remove(item.getNo());
		
		return "Successfully processed.";
	}
	
	@RequestMapping(value = "/create", method = RequestMethod.POST)
	public @ResponseBody String create(@ModelAttribute @Validated Link item, Errors errors, SessionStatus status, RedirectAttributes redirectAttributes) {
		if (errors.hasErrors()) {
			return "Failure";
		}
		
		service.create(item);
		
		return Integer.toString(item.getNo());
	}
	
	@RequestMapping(value = "/modify", method = RequestMethod.POST)
	public @ResponseBody String modify(@ModelAttribute @Validated Link item, Errors errors) {
		if (errors.hasErrors())
			return "Failure";
		
		service.modify(item);
			
		return "Success.";
	}
	
	@RequestMapping(value = "/countUp", method = RequestMethod.POST)
	public @ResponseBody String countUp(@ModelAttribute Link item, Errors errors) {
		if (errors.hasErrors())
			return "Failure";
		
		service.countUp(item.getNo());
			
		return "Success.";
	}

}
